﻿using SD.Infrastructure.EntityBase;
using SD.OpcProxy.ValueObjects;
using SD.OpcProxy.ValueObjects.Enums;
using System;
using System.Text;

namespace SD.OpcProxy.Domain.Entities
{
    /// <summary>
    /// OPC服务器
    /// </summary>
    public class OpcServer : AggregateRootEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected OpcServer() { }
        #endregion

        #region 01.创建OPC服务器构造器
        /// <summary>
        /// 创建OPC服务器构造器
        /// </summary>
        /// <param name="serverName">服务器名称</param>
        /// <param name="mode">OPC模式</param>
        /// <param name="url">服务器地址</param>
        /// <param name="pingInterval">Ping间隔</param>
        /// <param name="monitorInterval">监听间隔</param>
        /// <param name="timeout">超时时间</param>
        public OpcServer(string serverName, OpcMode mode, string url, int pingInterval, int monitorInterval, int timeout)
            : this()
        {
            #region # 验证

            if (timeout >= pingInterval)
            {
                throw new ArgumentOutOfRangeException(nameof(timeout), "超时时间不可大于Ping间隔！");
            }

            #endregion

            base.Number = url;
            base.Name = serverName;
            this.Mode = mode;
            this.PingInterval = pingInterval;
            this.MonitorInterval = monitorInterval;
            this.Timeout = timeout;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #endregion

        #region # 属性

        #region OPC模式 —— OpcMode Mode
        /// <summary>
        /// OPC模式
        /// </summary>
        public OpcMode Mode { get; private set; }
        #endregion

        #region Ping间隔 —— int PingInterval
        /// <summary>
        /// Ping间隔
        /// </summary>
        /// <remarks>单位：秒</remarks>
        public int PingInterval { get; private set; }
        #endregion

        #region 监听间隔 —— int MonitorInterval
        /// <summary>
        /// 监听间隔
        /// </summary>
        /// <remarks>单位：毫秒</remarks>
        public int MonitorInterval { get; private set; }
        #endregion

        #region 超时时间 —— int Timeout
        /// <summary>
        /// 超时时间
        /// </summary>
        /// <remarks>单位：秒</remarks>
        public int Timeout { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改OPC服务器 —— void UpdateInfo(string serverName, string url...
        /// <summary>
        /// 修改OPC服务器
        /// </summary>
        /// <param name="serverName">服务器名称</param>
        /// <param name="url">服务器地址</param>
        /// <param name="pingInterval">Ping间隔</param>
        /// <param name="monitorInterval">监听间隔</param>
        /// <param name="timeout">超时时间</param>
        public void UpdateInfo(string serverName, string url, int pingInterval, int monitorInterval, int timeout)
        {
            #region # 验证

            if (this.Id == Constants.OpcProxyServerId)
            {
                throw new InvalidOperationException("OPC代理服务器不可修改！");
            }
            if (timeout >= pingInterval)
            {
                throw new ArgumentOutOfRangeException(nameof(timeout), "超时时间不可大于Ping间隔！");
            }

            #endregion

            base.Number = url;
            base.Name = serverName;
            this.PingInterval = pingInterval;
            this.MonitorInterval = monitorInterval;
            this.Timeout = timeout;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #region 初始化关键字 —— void InitKeywords()
        /// <summary>
        /// 初始化关键字
        /// </summary>
        private void InitKeywords()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(base.Number);
            builder.Append(base.Name);

            base.SetKeywords(builder.ToString());
        }
        #endregion

        #endregion
    }
}
